package
{
	import eventdispatcher.JNEventDispatcher;
	
	import laya.events.Event;
	import laya.events.EventDispatcher;
	import laya.net.HttpRequest;
	import laya.utils.Browser;
	
	public class GetRequest extends EventDispatcher
	{
		public function GetRequest()
		{
			super();
		}
		public static function getRequests():Object{
			
			var url:String = Browser.window.location.search; //获取url中含"?"符后的字串
			
			var theRequest:Object = new Object();
			
			if (url.indexOf("?") != -1) {
				
				var str:String = url.substr(1);
				
				var strs:Array = str.split("&");
				
				for(var i:int = 0; i < strs.length; i ++) {
					
					theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
					
				}
			}
			return theRequest;
		}
		public static function sendRequest(url:String, data:*=null, method:String="get", responseType:String="text", headers:Array=null):void{
			var xhr:HttpRequest = new Laya.HttpRequest();
			xhr.http.timeout = 10000;//设置超时时间；
			xhr.once(Event.COMPLETE,null,completeHandler);
			xhr.once(Event.ERROR,null,errorHandler);
			xhr.on(Event.PROGRESS,null,processHandler);
			xhr.send(url,data,method,responseType,headers);
		}
		
		private static function completeHandler(data:Object):void
		{
			// TODO Auto Generated method stub
			JNEventDispatcher.getInstance().event(JNEventDispatcher.SEND_REQUEST_RESULT,data);
		}
		
		private static function processHandler(data:*):void
		{
			// TODO Auto Generated method stub
//			trace("process:"+data);
		}
		
		private static function errorHandler(data:*):void
		{
			// TODO Auto Generated method stub
			trace(data);
		}
	}
}